package org.wdcode.base.struts.result;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.wdcode.common.constants.HtmlConstants;

import org.wdcode.common.constants.StringConstants;
import org.wdcode.common.tools.Conversion;
import org.wdcode.common.util.CloseUtil;
import org.wdcode.common.util.CommonUtil;
import org.wdcode.web.util.ResponseUtil;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 返回结果弹出alert框
 * @author WD
 * @since JDK6
 * @version 1.0 2009-10-28
 */
public class AlertMessageResult extends StrutsResultSupport {
	// 序列化ID
	private static final long	serialVersionUID	= 854800907717510008L;

	/**
	 * 构造方法
	 */
	public AlertMessageResult() {
		super();
	}

	/**
	 * 构造方法
	 */
	public AlertMessageResult(String location) {
		super(location);
	}

	/**
	 * 构造方法
	 */
	public AlertMessageResult(String location, boolean parse, boolean encode) {
		super(location, parse, encode);
	}

	/**
	 * 实现方法
	 */
	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
		// 获得request
		HttpServletRequest request = ServletActionContext.getRequest();
		// 获得response
		HttpServletResponse response = ServletActionContext.getResponse();
		// 跳转的url
		StringBuilder url = new StringBuilder();

		// 判断是否有xx://头
		if (finalLocation.indexOf(StringConstants.COLON) == -1) {
			// 添加工程名
			url.append(Conversion.toString(request.getContextPath()));
			// 判断开始是否是'/'
			if (!finalLocation.startsWith(StringConstants.BACKSLASH)) {
				// 添加命名空间
				url.append(Conversion.toString(invocation.getProxy().getNamespace()));
				url.append(StringConstants.BACKSLASH);
			}
			// 添加地址
			url.append(finalLocation);
			// 获得编码后的地址
			finalLocation = response.encodeRedirectURL(url.toString());
		}

		// 设置头
		response.setContentType(HtmlConstants.TEXT_HTML_UTF_8);
		ResponseUtil.noCache(response);

		// 获得Action
		ActionSupport action = ((ActionSupport) invocation.getAction());
		// 声明返回信息
		StringBuilder message = new StringBuilder();
		// 获得字段错误
		Map<String, List<String>> fieldErrors = action.getFieldErrors();
		// 循环添加字段错误
		for (Map.Entry<String, List<String>> entry : fieldErrors.entrySet()) {
			// 循环错误列表添加信息
			message.append(getMessage(entry.getValue()));
		}
		// 添加Action错误
		message.append(getMessage(action.getActionErrors()));
		// 添加Action信息
		message.append(getMessage(action.getActionMessages()));

		// 输出到页面的javascript
		StringBuilder buffer = new StringBuilder();
		buffer.append("<script type=\"text/javascript\">");
		// 信息不为空
		if (!CommonUtil.isEmpty(message)) {
			buffer.append("alert('");
			buffer.append(message);
			buffer.append("');");
		}
		// 添加跳转页面
		buffer.append(" location='");
		buffer.append(finalLocation);
		buffer.append("';</script>");

		// 获得输出流
		PrintWriter out = response.getWriter();
		// 输出信息
		out.println(buffer.toString());
		// 关闭流
		CloseUtil.close(out);
	}

	/**
	 * 获得list中的信息
	 * @param list
	 * @return
	 */
	private String getMessage(Collection<String> c) {
		// 临时保存信息类
		StringBuilder sb = new StringBuilder();
		// 循环列表
		for (String s : c) {
			// 添加信息
			sb.append(s);
			// 添加回车
			sb.append(StringConstants.NEWLINE);
		}
		// 返回信息
		return sb.toString();
	}
}
